/* jshint esversion: 6 */
const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 修改密码
router.post('/user/altPwd', async ctx => {
    let formData = ctx.request.body.formData;
    let id = formData.id
    let pwd = formData.userpwd
    if(id) {
        await db.models['admin'].update({
            userpwd: pwd,
            uby: ctx.user.id,
            errCount: 0,
            utime: tool.getTime()
        }, {
            where: {
                id: id
            }
        })
        return  ctx.suc('已更新密码')
    }
    else {
        return ctx.err('请输入密码')
    }
})
router.post('/user/altMyPwd', async ctx => {
    let newPwd = ctx.request.body.newPwd;
    if(!newPwd) return ctx.err('请输入新密码')
    let id = ctx.user.id
  let user = await db.models['admin'].findOne({
      where: {
          tenantId: ctx.user.tenantId,
          id
      }
  })
    if(!user) return ctx.err('用户查询失败')
    user.userpwd = newPwd
    user.token = tool.uuid()
    await user.save()
    return ctx.suc('修改成功，将重新登录 ')
})

module.exports = router;